<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingConsumable;
use App\Models\DedustingDevice;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Handlers\IotCardHandler;

class ConsumableController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('耗材列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(DedustingConsumable::with(['device']), function (Grid $grid) {
            $grid->column('device.name', '设备');
            $grid->column('name', '名称');
            $grid->column('type', '类型');
            $grid->column('num', '数量');
            $grid->column('image', '图片')->image();
            $grid->column('expired_at', '失效日期');
            $grid->column('created_at', '创建日期');
            $grid->column('updated_at', '修改时间');
        });
    }

    public function show($id, Content $content)
    {
        return $content->header('耗材')
            ->description('详情')
            ->body(Show::make($id, DedustingConsumable::with(['device']), function (Show $show) {
                $show->field('device.name', '设备');
                $show->field('name', '名称');
                $show->field('type', '类型');
                $show->field('num', '数量');
                $show->field('image', '图片')->image();
                $show->field('expired_at', '失效日期');
                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('耗材')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new DedustingConsumable(), function (Form $form) {
            $devices = DedustingDevice::pluck('name','id');
            $form->select('device_id', '设备')->options($devices)->required();
            $form->text('name', '名称')->required();
            $form->text('type', '类型')->required();
            $form->number('num', '数量')->required();
            $form->image('image', '图片')->move(DedustingConsumable::IMAGE_PATH)->uniqueName()->saveFullUrl()->autoUpload()->required();

            $form->date('expired_at', '失效日期')->default(null);

            $form->hidden('user_id')->value(Admin::user()->id);

            $form->saving(function (Form $form) {
                $form->user_id = Admin::user()->id;
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('耗材编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}